Storing Containers
Learn how to store containers in a container registry to make them available for GitOps.
Running container workloads on Kubernetes requires container images to be stored within a repository where the container orchestration platform can easily access them. A container registry is commonly used to store and retrieve container images in cloud-native scenarios. There are many container registries for developers to choose from such as Docker Hub and Azure Container Registry.
Container images are normally created as part of a team's CD pipeline when an automated CI system builds the container and stores it within a container registry. Once stored in the container registry, the container image can be pulled by other developers or platforms like Kubernetes.
Container registry setup#
For this course, we'll keep things simple by manually storing container images within a public Docker Hub registry. Before proceeding, we'll need to create a free container registry on Docker Hub.
To store container images within the Docker Hub container registry, we'll need to create a repository for the container images. To create the repository, click the "Create repository" button on the Docker Hub landing page.
Then, create a new public repository named python-sample.
Note: Remember the Docker Hub configuration including username, password, registry name, and repository name because this information will be necessary to complete the lessons in the course.
Pushing containers to Docker Hub#
Now, we'll store the container image created in the last lesson within the new Docker Hub repository. Once the image is placed in the registry, it will become available for the Kubernetes platform to pull and run on the cluster.
To begin, launch the interactive terminal, which contains the updated dockerfile from the previous lesson. Within the interactive terminal, we'll complete the following steps to push a new container image to the registry:
Log in to DockerHub.
Build and tag the image.
Push the container image to Docker Hub.
Retrieve the container image from Docker Hub.
Here's a visual sequence of the steps we'll be completing:
Log in to Docker Hub#
To interact with the registry on Docker Hub, we'll need to authenticate with the credentials we created earlier. Execute the following command within the terminal of the interactive console, which will prompt us to enter our Docker Hub credentials for authentication:
After logging into Docker Hub via the command, we'll see the following output in the terminal of the interactive widget:
Build and tag the image#
To store the image in the repository on Docker Hub, we'll first need to build the image locally with the name of our new Docker registry and repository. Fill in the placeholders and execute the following command within the terminal to build the image in this manner:
After executing the commands to build the image, the following output will appear in the terminal:
Push the image#
Next, we'll push the container image from the local Docker registry to the registry we created on Docker Hub. Fill in the placeholders and execute the following command within the terminal to push the container image to Docker Hub:
After the command completes, we'll see the following output in the terminal of the interactive widget:
We can return to the Docker Hub repository via a web browser to view the container image within the "Tags" tab.
Now, the newly built container image is available within the container registry where it can be retrieved by Docker and run as a container.
Retrieve the image#
Now that the image is stored in Docker Hub, let's remove it from the local registry and test pulling it from the remote registry. Execute the following commands within the terminal of the interactive widget, replacing the placeholders with the Docker Hub username:
When the docker rmi command is executed, the following output will display on the console:
Then, we'll notice that the image has been removed from the local images by observing the output of the docker images command:
Finally, when we execute the command to run the container on line 8, we'll notice that the container image is pulled to the local registry from Docker Hub and launched by Docker.
This confirms that the container image is available to be pulled and run by any host running Docker. At this point, we could reference this container image in a declarative Kubernetes manifest to add the sample Python application within our system's desired state.
Test the application#
With the container launched, we can send a request to the application with curl to test the configuration. To execute the test, run the following command within a new interactive terminal:
After executing the curl command, we should receive a message from the application stating, Hello, Educative Learner! This is version 1.0.
Note: When sending traffic to the application, be sure to use a new interactive terminal to execute the
curlcommand.
Try it yourself#
/
Conclusion#
The container registry serves an important role within GitOps by making container images available to orchestrators like Kubernetes. We can now proceed with running the container image stored on Docker Hub within a Kubernetes cluster.
Containerizing an Application
Provisioning a Kubernetes Cluster